/*****************************************************************************/
/* @file     BIA.c
 * @author   FM    
 * @version  v0
 * @date     Septemper 2020
 * @brief    Simple function in C, Ansi-style

 * Copyright (C) 2020 Hangzhou SDIC Microelectronics Co., Ltd*/

/******************************************************************************/
 #include "BIA.h"
 
#if 0 
uint32_t SDADCDataBUF[10];
uint8_t adcconvertcount;
//stateFlag.adcupdata=0;
uint32_t disreg[3];

uint16_t LcdTable[]={ Disp_Num0,Disp_Num1,Disp_Num2,Disp_Num3,Disp_Num4,Disp_Num5,Disp_Num6,Disp_Num7,Disp_Num8,Disp_Num9,\
                       Disp_A,Disp_b,Disp_C_Big,Disp_d,Disp_E,Disp_F,Disp_G,Disp_H,Disp_I,Disp_J,Disp_K,Disp_L,\
					   Disp_M,Disp_N_Big,Disp_O_Big,Disp_P,Disp_q,Disp_r,Disp_S,Disp_T,Disp_U_Big,Disp_v,Disp_W,\
			           Disp_y,Disp_Z,\
			           Disp_NEG,Disp_Line_Top,Disp_Line_Bottom,Disp_BL};

void SDADCInit(void)
{
	IA_InitTypeDef SDADC_IAInitStruct;
	SDADC_IAInitStruct.IA_ExteralChopper=0;
	SDADC_IAInitStruct.IA_InteralChopper=0;
	SDADC_IAInitStruct.IA_VOS=0;
	SDADC_IAInitStruct.IA_OSEN=DISABLE;
	SDADC_IAInitStruct.IA_OSSIGN=ENABLE;
	SDADC_IAInitStruct.IA_IAD=ENABLE;
	SDADC_IAInitStruct.IA_RCF=IA_RCF_0;
	SDADC_IAInitStruct.IA_GAIN=IA_GAIN1;
	SDADC_IAInitStruct.IA_SIEN=DISABLE;
	SDADC_IAInitStruct.SDADC_IAINNS=SDADC_IAINNS_ACM;
	SDADC_IAInitStruct.SDADC_IAINPS=SDADC_IAINPS_Recap;
	SDADC_IAInit(&SDADC_IAInitStruct);
	
	SDADC_InitTypeDef SDADC_InitStruct;
	SDADC_InitStruct.SDADC_BUFFDS=SDADC_BUFFDS16;
	SDADC_InitStruct.SDADC_DCSET=SDADC_DCSET0;
	SDADC_InitStruct.SDADC_VREFG=SDADC_VREFG1_4;
	SDADC_InitStruct.SDADC_VREFS=SDADC_VREFS_AVDDRVSS;
	SDADC_InitStruct.SDADC_ASPCK1=256000;                      //单位Hz
	SDADC_InitStruct.SDADC_ADOUTS=ENABLE;
	SDADC_InitStruct.SDADC_OSR=SDADC_OSR16384;
	SDADC_InitStruct.SDADC_DIEN=DISABLE;
	SDADC_InitStruct.SDADC_PAEN=DISABLE;
	SDADC_Init(&SDADC_InitStruct);
	
	SDADC_Buf1Cmd(ENABLE);
	SDADC_Buf1Cmd(ENABLE);
}

void SINInit(void)
{
	RECS_InitTypeDef RECS_InitStruct;
	RECS_InitStruct.SIN_RECSP=SIN_RECSP_A2;
	RECS_InitStruct.SIN_RECSN=SIN_RECSP_A3;
	RECS_InitStruct.SIN_HYSEL=SIN_HYSEL_0mV;
	RECS_InitStruct.SINFEL=DISABLE;
	SIN_RECSInit(&RECS_InitStruct);
	
	SIN_A4SELConfig(SIN_A4SELA9,ENABLE);
	SIN_PhaseConfig(0,0);                     //配置PSP=PSN=0

	SIN_CLKConfig(SIN_IHRC_DIV6,SIN_50kHz);
	
    SIN_OPAOUTChannelConfig(SIN_OPAOUTA7,ENABLE);	
	OP_InitTypeDef OP_InitStruct;
	OP_InitStruct.OPPS=SIN_OPPS_VBG;
	OP_InitStruct.OPNS=SIN_OPNS_A2;
	OP_InitStruct.SIN_OPCSx=SIN_OPCSP;
	
	SIN_OPInit(SIN_OPA,&OP_InitStruct);
	
	SIN_Cmd(ENABLE);
	SIN_OPACmd(ENABLE);
}
void BIAInit(void)
{
	stateFlag.adcupdata=0;

	SINInit();
	
	LCD_StructInit();
	
	LCD_InitTypeDef LCD_InitStruct; 
	LCD_InitStruct.LCDPin_Mode_COM=0x0f;
	LCD_InitStruct.LCDPin_Mode_SEG1=0x0000fff0;
	LCD_InitStruct.LCDPin_Mode_SEG2=0;	
	LCD_Init(&LCD_InitStruct);
	
	SDADCInit();
	SDADC_Cmd(ENABLE);	
}

void BIAImpedanceMeasure(void)	
{
	stateFlag.adcupdata=0;
	SDADC_ClearITStatus();
	SDADC_ITCmd(ENABLE);
	SIN_RECSChannelConfig(SIN_RECSP_A2,SIN_RECSP_A3);
	while(stateFlag.adcupdata)
	{
		stateFlag.adcupdata=0;
		SDADCDataBUF[1]=SDADCDataBUF[0];        //缓存300R阻值	
		SIN_RECSChannelConfig(SIN_RECSP_A2,SIN_RECSP_A4);		
		SDADC_ClearITStatus();
		SDADC_ITCmd(ENABLE);

	}
	while(stateFlag.adcupdata)
	{
		stateFlag.adcupdata=0;
		SDADCDataBUF[2]=SDADCDataBUF[0];        //缓存600R阻值	
		SIN_RECSChannelConfig(SIN_RECSP_A5,SIN_RECSP_A6);		
		SDADC_ClearITStatus();
		SDADC_ITCmd(ENABLE);
		while(stateFlag.adcupdata)
		{
			SDADCDataBUF[3]=SDADCDataBUF[0];        //缓存A5_A6阻值					
			BIADataProcess();
		}
	}
}

void BIADataProcess(void)
{
	SDADCDataBUF[4]=SDADCDataBUF[2]-SDADCDataBUF[1];
	SDADCDataBUF[5]=SDADCDataBUF[3]-SDADCDataBUF[1];
	SDADCDataBUF[6]=SDADCDataBUF[5]*3300/SDADCDataBUF[4]+3300;
}

void BIADataDisplay(void)
{
	
	if(!GPIO_ReadInputDataBit(GPIO0, GPIO_Pin_1))     //显示300内码
	{
		ConverttoDispCode(SDADCDataBUF[1]);
		Display();
	}
	if(!GPIO_ReadInputDataBit(GPIO0, GPIO_Pin_2))     //显示600内码
	{
		ConverttoDispCode(SDADCDataBUF[2]);
		Display();
	}
	if(!GPIO_ReadInputDataBit(GPIO0, GPIO_Pin_3))     //显示Rx内码
	{
		ConverttoDispCode(SDADCDataBUF[3]);
		Display();;
	}
	if(!GPIO_ReadInputDataBit(GPIO0, GPIO_Pin_4))     //显示600-300内码
	{
		ConverttoDispCode(SDADCDataBUF[4]);
		Display();
	}
	
	if(!GPIO_ReadInputDataBit(GPIO0, GPIO_Pin_5))     //显示Rx-300内码
	{
		ConverttoDispCode(SDADCDataBUF[5]);
		Display();
	}
	if(!GPIO_ReadInputDataBit(GPIO0, GPIO_Pin_6))     //显示Rx阻值
	{
		ConverttoDispCode(SDADCDataBUF[6]);
		Display();
	}	
}

void BIAInternalCodeDisp(void)
{
	stateFlag.adcupdata=0;
	if(!GPIO_ReadInputDataBit(GPIO4, GPIO_Pin_0))     //显示300R阻值
	{	
		SIN_RECSChannelConfig(SIN_RECSP_A2,SIN_RECSP_A3);	
	}
	if(!GPIO_ReadInputDataBit(GPIO4, GPIO_Pin_1))     //显示600R阻值
	{	
		SIN_RECSChannelConfig(SIN_RECSP_A2,SIN_RECSP_A4);	
	}		
	SIN_RECSChannelConfig(SIN_RECSP_A5,SIN_RECSP_A6);	//显示Rx阻值
	
	SDADC_ClearITStatus();
	SDADC_ITCmd(ENABLE);
	
	while(stateFlag.adcupdata)
	{
		stateFlag.adcupdata=0;
		ConverttoDispCode(SDADCDataBUF[0]);
		Display();
	}

}


void Display(void)
{
	
	LCD_DRConfig(LCD_DR1,disreg[0]);
	LCD_DRConfig(LCD_DR2,disreg[1]);
	LCD_DRConfig(LCD_DR3,disreg[2]);
	
}

void ConverttoDispCode(uint32_t data)      //N
{
	
	uint8_t  disreg1=0;
	uint16_t disreg2=0;
//	uint16_t disreg3=0;
	
	disreg[0]=0;       //清24位ADC低字节显码
	disreg[1]=0;       //清24位ADC中字节显码
	disreg[2]=0;       //清24位ADC高字节显码

	uint32_t tmpreg=data;
	
	for(uint8_t j=0;j<3;)
	{
		for(uint8_t i=0;i<2;)
		{
			disreg1=tmpreg>>=i*4;
			disreg1=tmpreg&0x0f;

			disreg2 = LcdTable[disreg1];
			//disreg3 = 0;
			disreg[j]|= (disreg2<<(i*16));
			i++;
		}
		tmpreg>>=4;
		j++;
	}
	
}

#endif